package com.zeng.sim.util;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-21T16:46
 */
public class ServletUtil {

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getResponse();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static boolean isAjax(HttpServletRequest request) {
        String requestType = request.getHeader("X-Requested-With");
        if ("XMLHttpRequest".equals(requestType)) {
            return true;
        }
        else {
            return false;
        }
    }

    public static void write(String json) throws IOException {
        HttpServletResponse response = getResponse();
        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
        response.getWriter().write(json);
    }

    public static String getQueryString() {
        return getRequest().getQueryString();
    }

    public static String getRequestURI() {
        return getRequest().getRequestURI();
    }

    public static String getRemoteHost() {
        String remoteHost = getRequest().getRemoteHost();
        if ("0:0:0:0:0:0:0:1".equals(remoteHost)) {
            remoteHost = "127.0.0.1";
        }
        return remoteHost;
    }

    public static String getMethod() {
        return getRequest().getMethod();
    }

    public static String getHeader(String name) {
        return getRequest().getHeader(name);
    }

    public static String getUserAgent() {
        return getHeader(HttpHeaders.USER_AGENT);
    }

    public static String getBrowserName() {
        String userAgent = getUserAgent();
        if (userAgent.contains("Firefox")) {
            return "火狐浏览器";
        }
        else if (userAgent.contains("Chrome")) {
            return "谷歌浏览器";
        }
        else if (userAgent.contains("Trident")) {
            return "IE 浏览器";
        }
        else {
            return "未知浏览器";
        }
    }

    public static String getSystem() {
        String userAgent = getUserAgent().toLowerCase();
        if (userAgent.contains("windows")) {
            return "Windows";
        }
        else if (userAgent.contains("mac")) {
            return "Mac";
        }
        else if (userAgent.contains("x11")) {
            return "Unix";
        }
        else if (userAgent.contains("android")) {
            return "Android";
        }
        else if (userAgent.contains("iphone")) {
            return "IPhone";
        }
        else {
            return "UnKnown, More-Info: " + userAgent;
        }
    }

}
